// Copyright (C) 2007 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/* Insert a [Top] link at the specified headers. The default is "H2". 
 * The script runs when the page loads.
 *
 * Example: 
 * Before:
 *    <h2>The Old Content</h2>
 * After
 *    <h2>The Old Content <span class="navtop"><a href="#top">[Top]</a></span></h2> 
 *
 *    * navtop: Class that specifies the appearance of the link in the text. You must
 *              define this class appropriately, such as:
 *              .navtop {font-size: xx-small; float:right;}
 *              You must include a stylesheet that defines this class 
 *              or define the style for the class yourself, such as:
 *              .navtop {font-size: xx-small; float:right;}
 *    * #top: The id of the element to jump to. If you do not explicitely put id="top" on one 
 *            of your elements, this script adds it to the <BODY> element
 *  		  and overrides any id value that was already there.
 *            A good way to set the "top" is by inserting the following
 *            code where you want to jump to when [top] is clicked.
 *           <a name="top" id="top"></a>
 *    * [Top]: The visible text of the inserted link. 
 *
 * By default, the script creates [Top] links for all H2.
 * You can specify a different scheme using the "name" attribute in the
 * same element that specifies where the top is. 
 * Example: <a  name="h1h2h3" id="top"></a>
 * The following options are recognized: "h1", "h3", "h1h2", "h2h3", and "h1h2h3"
 *  
 * Note: The script omits the first h1 since it is often 
 *       the title of the document and always too close to the top.
 *
 */

GTT_addEvent(window, "load", GTT_gotoInit);

// GTT_addEvent function -- from sorttable.js
function GTT_addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

// Loop through all the headers of a given tag
function GTT_loopHeaders(headers, skip) 
{
  if (!headers) return; //headers, nothing to do
  
        for (var i = skip, header; header = headers[i]; i++) {
        var span = GTT_createSpan(); // create the node to add
        header.appendChild(span);
       } 
}


function GTT_gotoInit() {
  
  if (!document.getElementsByTagName) return; // no browser support 
  
  // Make sure there's a top to jump to.
  whichHeaders = GTT_findTop(); 
  
  switch(whichHeaders) {
      case "h1":     
        GTT_loopHeaders(document.getElementsByTagName("h1"), 1); 
        break;
      case "h3":     
        GTT_loopHeaders(document.getElementsByTagName("h3"), 0); 
        break;
      case "h1h2":   
        GTT_loopHeaders(document.getElementsByTagName("h1"), 1); 
        GTT_loopHeaders(document.getElementsByTagName("h2"), 0); 
        break; 
      case "h2h3":   
        GTT_loopHeaders(document.getElementsByTagName("h2"), 0);  
        GTT_loopHeaders(document.getElementsByTagName("h3"), 0); 
        break;
      case "h1h2h3": 
        GTT_loopHeaders(document.getElementsByTagName("h1"), 1); 
        GTT_loopHeaders(document.getElementsByTagName("h2"), 0); 
        GTT_loopHeaders(document.getElementsByTagName("h3"), 0); 
        break;
      default:       
        GTT_loopHeaders(document.getElementsByTagName("h2"), 0);
  }
}

// If some element already has the id "top", just return.
// Otherwise, add that id to the <BODY> element.
function GTT_findTop() {
  var top = document.getElementById("top");
  if (top == null) {
    document.body.id="top";
	return "h2";
  }
  else {return (top.name != null ? top.name : "h2" ); } //name specifies what we are doing
}

// This function creates the span element and its contents:
// <span class="navtop"><a href="#top">[Top]</a></span>
function GTT_createSpan() {
  var span = document.createElement("span");
  span.className = "navtop";
  var link = document.createElement("a");
  span.appendChild(link);
  link.href = "#top";
  var textNode = document.createTextNode("[Top]");
  link.appendChild(textNode);

  return span;
}
